package com.mine.consumer.controller;


import com.mine.consumer.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @功能
 * @创建时间 2020/4/13
 */
@RestController
@RequestMapping("/ask")
public class AskController {
	//注入restTemplate对象
	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 注入DiscoveryClient :
	 *  springcloud提供的获取原数组的工具类
	 *      调用方法获取服务的元数据信息
	 *
	 */
	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping(value = "/buy1/{id}",method = RequestMethod.GET)
	public Employee findById(@PathVariable Long id) {
		//调用discoveryClient方法
		//已调用服务名称获取所有的元数据
		List<ServiceInstance> instances = discoveryClient.getInstances("ability-service");
		//获取唯一的一个元数据
		ServiceInstance instance = instances.get(0);
		//根据元数据中的主机地址和端口号拼接请求微服务的URL
		Employee product = null;
		//如何调用商品服务?
	//	product = restTemplate.getForObject("http://127.0.0.1"+":"+9011+"/ablivity/1",Employee.class);
		//		List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

	//	product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/ablivity/1",Employee.class);
		product = restTemplate.getForObject("http://ability-service/ablivity/1",Employee.class);

		return product;
	}
	@RequestMapping(value = "{id}",method = RequestMethod.GET)
	public String www(){
		return "好烦啊";
	}
}
